package com.xuexiang.xuidemo.page.uilts.component;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.xuexiang.xui_lib.util.ViewUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;
import com.xuexiang.xuidemo.util.DisplayUtils;

import static com.xuexiang.xui_lib.util.ViewUtils.Direction.BOTTOM_TO_TOP;
import static com.xuexiang.xui_lib.util.ViewUtils.Direction.LEFT_TO_RIGHT;
import static com.xuexiang.xui_lib.util.ViewUtils.Direction.TOP_TO_BOTTOM;

/**
 * 控件动画
 *
 * @since 2021-03-25
 */
@Page(name = "控件动画", action = "utils.WidgetAnimationAbilitySlice", menuRes = Page.MENU)
public class WidgetAnimationAbilitySlice extends BasePageAbilitySlice implements Component.ClickedListener {
    private static final int DURATION = 2000;
    private static final long DEFAULT_DELAY_TIME = 10L;
    private static final int OFFY = 42;
    private static final int BG_COLOR = 0xFFFFFF00;
    private Text target;
    private Component root;
    private BaseDialog baseDialog;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_utils_widget_animation;
    }

    @Override
    protected void onMenuClick(Component component) {
        DirectionalLayout itemView = (DirectionalLayout) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_dialog_utils_widget_anim_text, null, false);
        baseDialog = new CommonDialog(getContext())
            .setContentCustomComponent(itemView)
            .setAutoClosable(true)
            // fixme 弹框从底部弹出, 疑似属于系统bug 暂时设置向下偏移量
            .setOffset(0, DisplayUtils.vp2px(getContext(), OFFY))
            .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
            .setCornerRadius(0)
            .setAlignment(LayoutAlignment.BOTTOM);
        itemView.findComponentById(ResourceTable.Id_anim_fade).setClickedListener(this);
        itemView.findComponentById(ResourceTable.Id_anim_slide1).setClickedListener(this);
        itemView.findComponentById(ResourceTable.Id_anim_slide2).setClickedListener(this);
        itemView.findComponentById(ResourceTable.Id_anim_bg).setClickedListener(this);
        baseDialog.show();
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        target = findCastComponentById(ResourceTable.Id_target);
        root = findComponentById(ResourceTable.Id_root);
    }

    @Override
    public void onClick(Component component) {
        int duration = DURATION;
        switch (component.getId()) {
            case ResourceTable.Id_anim_fade:
                if (target.getVisibility() == Component.HIDE || target.getVisibility() == Component.INVISIBLE) {
                    target.setText("以 Fade 动画显示本浮层");
                    ViewUtils.fadeIn(target, duration, null);
                } else {
                    target.setText("以 Fade 动画隐藏本浮层");
                    ViewUtils.fadeOut(target, duration, null);
                }
                break;
            case ResourceTable.Id_anim_slide1:
                if (target.getVisibility() == Component.HIDE || target.getVisibility() == Component.INVISIBLE) {
                    target.setAlpha(0F);
                    target.setText("以 Slide（上进上出）动画显示本浮层");
                    ViewUtils.slideIn(target, duration, null, TOP_TO_BOTTOM);
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> target.setAlpha(1f),
                        DEFAULT_DELAY_TIME);
                } else {
                    target.setAlpha(1F);
                    target.setText("以 Slide（上进上出）动画隐藏本浮层");
                    ViewUtils.slideOut(target, duration, null, BOTTOM_TO_TOP);
                }
                break;
            case ResourceTable.Id_anim_slide2:
                if (target.getVisibility() == Component.HIDE || target.getVisibility() == Component.INVISIBLE) {
                    target.setAlpha(0f);
                    ViewUtils.slideIn(target, duration, null, LEFT_TO_RIGHT);
                    target.setText("以 Slide（左进右出）动画显示本浮层");
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> target.setAlpha(1f),
                        DEFAULT_DELAY_TIME);
                } else {
                    target.setAlpha(1F);
                    target.setText("以 Slide（左进右出）动画隐藏本浮层");
                    ViewUtils.slideOut(target, duration, null, LEFT_TO_RIGHT);
                }
                break;
            case ResourceTable.Id_anim_bg:
                ViewUtils.playBackgroundBlinkAnimation(root, BG_COLOR);
                break;
            default:
                // ignore
        }
        if (baseDialog != null) {
            baseDialog.hide();
        }
    }
}
